home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
os2
/
e33el2.zip
/
emacs
/
19.33
/
lisp
/
mpuz.el
< prev
next >
Wrap
Lisp/Scheme
|
1996-01-20
|
15KB
|
444 lines
;;; mpuz.el --- multiplication puzzle for GNU Emacs
;; Copyright (C) 1990 Free Software Foundation, Inc.
;; Author: Philippe Schnoebelen <phs@lifia.imag.fr>
;; Keywords: games
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; When this package is loaded, `M-x mpuz' generates a random multiplication
;; puzzle. This is a multiplication example in which each digit has been
;; consistently replaced with some letter. Your job is to reconstruct
;; the original digits. Type `?' while the mode is active for detailed help.
;;; Code:
(random t) ; randomize
(defvar mpuz-silent nil
"*Set this to T if you don't want dings on inputs.")
(defun mpuz-ding ()
"Dings, unless global variable `mpuz-silent' forbids it."
(or mpuz-silent (ding t)))
;; Mpuz mode and keymaps
;;----------------------
(defvar mpuz-mode-hook nil)
(defvar mpuz-mode-map nil
"Local keymap to use in Mult Puzzle.")
(if mpuz-mode-map nil
(setq mpuz-mode-map (make-sparse-keymap))
(define-key mpuz-mode-map "a" 'mpuz-try-letter)
(define-key mpuz-mode-map "b" 'mpuz-try-letter)
(define-key mpuz-mode-map "c" 'mpuz-try-letter)
(define-key mpuz-mode-map "d" 'mpuz-try-letter)
(define-key mpuz-mode-map "e" 'mpuz-try-letter)
(define-key mpuz-mode-map "f" 'mpuz-try-letter)
(define-key mpuz-mode-map "g" 'mpuz-try-letter)
(define-key mpuz-mode-map "h" 'mpuz-try-letter)
(define-key mpuz-mode-map "i" 'mpuz-try-letter)
(define-key mpuz-mode-map "j" 'mpuz-try-letter)
(define-key mpuz-mode-map "A" 'mpuz-try-letter)
(define-key mpuz-mode-map "B" 'mpuz-try-letter)
(define-key mpuz-mode-map "C" 'mpuz-try-letter)
(define-key mpuz-mode-map "D" 'mpuz-try-letter)
(define-key mpuz-mode-map "E" 'mpuz-try-letter)
(define-key mpuz-mode-map "F" 'mpuz-try-letter)
(define-key mpuz-mode-map "G" 'mpuz-try-letter)
(define-key mpuz-mode-map "H" 'mpuz-try-letter)
(define-key mpuz-mode-map "I" 'mpuz-try-letter)
(define-key mpuz-mode-map "J" 'mpuz-try-letter)
(define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
(define-key mpuz-mode-map "?" 'describe-mode))
(defun mpuz-mode ()
"Multiplication puzzle mode.
You have to guess which letters stand for which digits in the
multiplication displayed inside the `*Mult Puzzle*' buffer.
You may enter a guess for a letter's value by typing first the letter,
then the digit. Thus, to guess that A=3, type A 3.
To leave the game to do other editing work, just switch buffers.
Then you may resume the game with M-x mpuz.
You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
(interactive)
(setq major-mode 'mpuz-mode
mode-name "Mult Puzzle")
(use-local-map mpuz-mode-map)
(run-hooks 'mpuz-mode-hook))
;; Some variables for statistics
;;------------------------------
(defvar mpuz-nb-errors 0
"Number of errors made in current game.")
(defvar mpuz-nb-completed-games 0
"Number of games completed.")
(defvar mpuz-nb-cumulated-errors 0
"Number of errors made in previous games.")
;; Some variables for game tracking
;;---------------------------------
(defvar mpuz-in-progress nil
"True if a game is currently in progress.")
(defvar mpuz-found-digits (make-vector 10 nil)
"A vector recording which digits have been decrypted.")
(defmacro mpuz-digit-solved-p (digit)
(list 'aref 'mpuz-found-digits digit))
;; A puzzle uses a permutation of [0..9] into itself.
;; We use both the permutation and its inverse.
;;---------------------------------------------------
(defvar mpuz-digit-to-letter (make-vector 10 0)
"A permutation from [0..9] to [0..9].")
(defvar mpuz-letter-to-digit (make-vector 10 0)
"The inverse of mpuz-digit-to-letter.")
(defmacro mpuz-to-digit (letter)
(list 'aref 'mpuz-letter-to-digit letter))
(defmacro mpuz-to-letter (digit)
(list 'aref 'mpuz-digit-to-letter digit))
(defun mpuz-build-random-perm ()
"Initialize puzzle coding with a random permutation."
(let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
(index 10)
elem)
(while letters
(setq elem (nth (random index) letters)
letters (delq elem letters)
index (1- index))
(aset mpuz-digit-to-letter index elem)
(aset mpuz-letter-to-digit elem index))))
;; A puzzle also uses a board displaying a multiplication.
;; Every digit appears in the board, crypted or not.
;;------------------------------------------------------
(defvar mpuz-board (make-vector 10 nil)
"The board associates to any digit the list of squares where it appears.")
(defun mpuz-put-digit-on-board (number square)
"Put (last digit of) NUMBER on SQUARE of the puzzle board."
;; i.e. push SQUARE on NUMBER square-list
(setq number (% number 10))
(aset mpuz-board number (cons square (aref mpuz-board number))))
(defun mpuz-check-all-solved ()
"Check whether all digits have been solved. Return t if yes."
(catch 'found
(let ((digit -1))
(while (> 10 (setq digit (1+ digit)))
(if (and (not (mpuz-digit-solved-p digit)) ; unsolved
(aref mpuz-board digit)) ; and appearing in the puzzle !
(throw 'found nil))))
t))
;; To build a puzzle, we take two random numbers and multiply them.
;; We also take a random permutation for encryption.
;; The random numbers are only use to see which digit appears in which square
;; of the board. Everything is stored in individual squares.
;;---------------------------------------------------------------------------
(defun mpuz-random-puzzle ()
"Draw random values to be multiplied in a puzzle."
(mpuz-build-random-perm)
(fillarray mpuz-board nil) ; erase the board
(let (A B C D E)
;; A,B,C,D & E, are the five rows of our multiplication.
;; Choose random values, discarding uninteresting cases.
(while (progn
(setq A (random 1000)
B (random 100)
C (* A (% B 10))
D (* A (/ B 10))
E (* A B))
(or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D
;; Individual digits are now put on their respective squares.
;; [NB: A square is a pair <row,column> of the screen.]
(mpuz-put-digit-on-board A '(2 . 9))
(mpuz-put-digit-on-board (/ A 10) '(2 . 7))
(mpuz-put-digit-on-board (/ A 100) '(2 . 5))
(mpuz-put-digit-on-board B '(4 . 9))
(mpuz-put-digit-on-board (/ B 10) '(4 . 7))
(mpuz-put-digit-on-board C '(6 . 9))
(mpuz-put-digit-on-board (/ C 10) '(6 . 7))
(mpuz-put-digit-on-board (/ C 100) '(6 . 5))
(mpuz-put-digit-on-board (/ C 1000) '(6 . 3))
(mpuz-put-digit-on-board D '(8 . 7))
(mpuz-put-digit-on-board (/ D 10) '(8 . 5))
(mpuz-put-digit-on-board (/ D 100) '(8 . 3))
(mpuz-put-digit-on-board (/ D 1000) '(8 . 1))
(mpuz-put-digit-on-board E '(10 . 9))
(mpuz-put-digit-on-board (/ E 10) '(10 . 7))
(mpuz-put-digit-on-board (/ E 100) '(10 . 5))
(mpuz-put-digit-on-board (/ E 1000) '(10 . 3))
(mpuz-put-digit-on-board (/ E 10000) '(10 . 1))))
;; Display
;;--------
(defconst mpuz-framework
"
. . .
Number of errors (this game): 0
x . .
-------
. . . .
Number of completed games: 0
. . . .
--------- Average number of errors: 0.00
. . . . ."
"The general pi